/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "nci_feature.h"
#include "nci_dispater.h"
#include "nfc_log_api.h"
#include "nci_core.h"
#include "nci_rf.h"
#include "nci_nfcee.h"
#include "nci_data.h"
#include "nci_proprietary.h"

/**
 * @file nci_dispater.c
 * @brief NCI消息分发器，处理nci_transport.c中未处理的NCI消息
*/

static void nci_process_rsp(uint8_t gid, uint8_t oid, uint8_t *payload, uint16_t len);
static void nci_process_ntf(uint8_t gid, uint8_t oid, uint8_t *payload, uint16_t len);

/**
 * @brief 分发未处理的NCI消息
 *
 * @param buf 需要分发的数据
*/
void nci_process_dispatch(tNCI_BUF *buf) {
    if (!buf) {
        return;
    }
    NFCLOG_D("nci_process_dispatch: len=%d", buf->len);

    uint8_t *p = buf->data;
    uint8_t mt= buf->head.mt;
    uint8_t gid = buf->head.gid;
    uint8_t oid = buf->head.oid;
    uint16_t paylaod_len = buf->len;
    oid |= (buf->head.rfu_h << 7);
    oid |= (buf->head.rfu_l << 6);
    switch (mt) {
        case NCI_RSP:
            nci_process_rsp(gid, oid, p, paylaod_len);
            break;
        case NCI_NTF:
            nci_process_ntf(gid, oid, p, paylaod_len);
            break;
        case NCI_DATA:
            nci_data_dispatch(gid, p, paylaod_len);
            break;
        default:
            NFCLOG_W("process unexpected MT types: %d", mt);
            break;
    }
}

static void nci_process_rsp(uint8_t gid, uint8_t oid, uint8_t *payload, uint16_t len) {
    (void) gid;
    (void) oid;
    (void) payload;
    (void) len;
    NFCLOG_W("process unexpected RSP");
    // TODO
}
static void nci_process_ntf(uint8_t gid, uint8_t oid, uint8_t *payload, uint16_t len) {
    switch (gid) {
        case NCI_CORE:
            nci_core_ntf_handle(oid, payload, len);
            break;
        case NCI_RF:
            nci_rf_ntf_handle(oid, payload, len);
            break;
        case NCI_NFCEE:
#if IS_FEATURE_ON(NCI_FEATURE_EE)
            nci_nfcee_ntf_handle(oid, payload, len);
#endif
            break;
        case NCI_PROPRIETARY:
#if IS_FEATURE_ON(NCI_FEATURE_VS)
            nci_proprietary_ntf_handle(oid, payload, len);
#endif /* IS_FEATURE_ON(NCI_FEATURE_VS) */
            break;
        default:
            NFCLOG_W("process unexpected OID: %d", oid);
            break;
    }
}
